package com.bigdatan.b2c.controller.glass;

import com.bigdatan.b2c.controller.AbstractController;
import com.bigdatan.b2c.controller.glass.vo.CategoryVO;
import com.bigdatan.b2c.entity.ProductCategory;
import com.bigdatan.b2c.service.IProductCategoryService;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import util.JsonResponse;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by shangdong on 17/5/13.
 */
@RestController
@RequestMapping("/api")
public class GlassCategoryController extends AbstractController {

    @Resource
    private IProductCategoryService productCategoryService;

    public static final Function<ProductCategory, CategoryVO> productCategoryCategoryVOFunction = productCategory -> {
        CategoryVO categoryVO = new CategoryVO();
        BeanUtils.copyProperties(productCategory, categoryVO);
        return categoryVO;
    };

    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/navigation")
    public JsonResponse<List<CategoryVO>> getAllCategory() {
        CategoryContext a = new CategoryContext();
        getCategory(0, a);
        return JsonResponse.successResp(a.getAllCategory());
    }

    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/getCategoryById")
    public JsonResponse<CategoryVO> getCategoryById(Integer categoryID) {
        CategoryContext a = new CategoryContext();
        getCategory(categoryID,a);
        return JsonResponse.successResp(a.getFindIDEq());
    }

    public void getCategory(Integer categoryID,CategoryContext categoryContext){
        CategoryVO resCate = new CategoryVO();
        List<ProductCategory> productCategoryList = productCategoryService
                .getAllNormalFirstLevelCategory();
        List<CategoryVO> categoryVOs;
        categoryVOs = Lists.transform(productCategoryList, productCategoryCategoryVOFunction);
        categoryVOs = Lists.newArrayList(categoryVOs);
        Comparator<CategoryVO> categoryVOComparator = (o1, o2) -> new Integer(o1.getCategoryId()).compareTo(new Integer(o2.getCategoryId()));
        for (CategoryVO categoryVO : categoryVOs) {
            if (categoryVO.getCategoryId() == categoryID){
                resCate = categoryVO;
            }
            List<ProductCategory> childs = productCategoryService.getAllNormalChildCategoryByCategoryId(categoryVO.getCategoryId());
            List<CategoryVO> childsVO = Lists.transform(childs, productCategoryCategoryVOFunction);
            List<CategoryVO> childsVOList = Lists.newArrayList(childsVO);
            for (CategoryVO vo : childsVOList) {
                if (vo.getCategoryId() == categoryID){
                    resCate = vo;
                    break;
                }
            }
            Collections.sort(childsVOList, categoryVOComparator);
            categoryVO.setChilds(childsVOList);
        }
        categoryContext.setFindIDEq(resCate);
        categoryContext.setAllCategory(categoryVOs);
    }

    @Data
    public static class CategoryContext{
        private CategoryVO findIDEq;
        private List<CategoryVO> allCategory;

    }


}
